package io.renren.modules.noc.service.impl;

import io.renren.modules.noc.dao.NocBeerSeatDao;
import io.renren.modules.noc.entity.NocBeerSeatEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


import java.util.*;


import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;

import io.renren.modules.noc.dao.NocBeerTicketMemberDao;
import io.renren.modules.noc.entity.NocBeerTicketMemberEntity;
import io.renren.modules.noc.service.NocBeerTicketMemberService;
import org.springframework.transaction.annotation.Transactional;


@Service("nocBeerTicketMemberService")
public class NocBeerTicketMemberServiceImpl extends ServiceImpl<NocBeerTicketMemberDao, NocBeerTicketMemberEntity> implements NocBeerTicketMemberService {

    @Autowired
    private NocBeerTicketMemberDao nocBeerTicketMemberDao;

    @Value("${ercode.beerPre}")
    private String _beerPre;

    @Autowired
    private NocBeerSeatDao nocBeerSeatDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<NocBeerTicketMemberEntity> page = this.page(
                new Query<NocBeerTicketMemberEntity>().getPage(params),
                new QueryWrapper<NocBeerTicketMemberEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public void in(Map<String, Object> map) {

        nocBeerTicketMemberDao.in(map);
    }

    @Override
    public void out(Map<String, Object> map) {

        nocBeerTicketMemberDao.out(map);
    }

    @Override
    public Date getInTime(Map<String, Object> map) {

        return nocBeerTicketMemberDao.getInTime(map);
    }

    @Override
    public Date getOutTime(Map<String, Object> map) {

        return nocBeerTicketMemberDao.getOutTime(map);
    }

    @Override
    public String getTotalMember(Map<String, Object> map) {

        return nocBeerTicketMemberDao.getTotalMember(map);
    }

    @Override
    public String getTotalCount(Map<String, Object> map) {

        return nocBeerTicketMemberDao.getTotalCount(map);
    }


    @Override
    public String getNextErCodeBeer() {
        Random r = new Random();
        int n2 = r.nextInt(999999);
        String s = String.valueOf(n2);
        if (s.length() < 6) {
            for (int j = 0; j < 6 - s.length(); j++) {
                s = "0" + s;
            }
        }
        Map<String, Object> map = new HashMap<>();
        map.put("ercode", "_beerPre" + s);
        Integer count = nocBeerTicketMemberDao.getCount(map);
        while (count != 0) {
            n2 = r.nextInt(999999);
            s = String.valueOf(n2);
            if (s.length() < 6) {
                for (int j = 0; j < 6 - s.length(); j++) {
                    s = "0" + s;
                }
            }
            Map<String, Object> map1 = new HashMap<>();
            map1.put("ercode", "_beerPre" + s);
            count = nocBeerTicketMemberDao.getCount(map1);

        }
        return _beerPre + s;

    }

    @Override
    public List<NocBeerTicketMemberEntity> getNocBeerTicketMemberEntity() {
        return nocBeerTicketMemberDao.getNocBeerTicketMemberEntity();
    }

    @Override
    @Transactional
    public Map<String, Object> giveTicket(String mobile,String ercode, String tid, String area, String tableRow, String tableNum, String validDate) {
        Map<String,Object> result = new HashMap<>();
        Map<String, Object> param = new HashMap<>();
        param.put("mobile",mobile);
        param.put("valid_date",validDate);
        //查询会员啤酒节门票是否存在
        List<NocBeerTicketMemberEntity> list2 = nocBeerTicketMemberDao.selectByMap(param);
        if(list2.size()>0){
            NocBeerTicketMemberEntity ticket = list2.get(0);
            Map<String,Object> map = new HashMap<>();
            map.put("flag",true);
            map.put("id",ticket.getId());
            map.put("validDate",ticket.getValidDate());
            return map;
        }
        //再次验证当前桌是否有剩余位置
        Map<String, Object> p = new HashMap<>();
        p.put("area", area);
        p.put("table_row", tableRow);
        p.put("table_num", tableNum);
        p.put("valid_date", validDate);
        List<NocBeerSeatEntity> list = nocBeerSeatDao.getNofpSeatNum(p);
        if (list != null && list.size() > 0) {
            //给观众发放门票
            NocBeerTicketMemberEntity ticket = new NocBeerTicketMemberEntity();
            //ticket.setMemberId(Integer.parseInt(mid));
            ticket.setErcode(ercode);
            ticket.setValidDate(new Date());
            ticket.setTid(Integer.parseInt(tid));
            ticket.setTimes(0);
            ticket.setRefreshTime(new Date());
            ticket.setMobile(mobile);
            nocBeerTicketMemberDao.insert(ticket);
            //绑定座位
            NocBeerSeatEntity t = list.get(0);
            t.setFid(ticket.getId());
            nocBeerSeatDao.updateById(t);

            result.put("flag",true);
            result.put("id",ticket.getId());
            result.put("validDate",ticket.getValidDate());
        }else{
            //无剩余位置
            result.put("flag",false);
        }
        return result;

    }

    @Override
    public String getAllCount(Map<String,Object> map){
        return nocBeerTicketMemberDao.getAllCount(map);
    }


    @Autowired
    public String getIsSignById(Map<String,Object> map){
        return nocBeerTicketMemberDao.getIsSignById(map);
    }

    @Override
    public Map<String,Object> getIsSign(Map<String,Object> map){
        return nocBeerTicketMemberDao.getIsSign(map);
    }


}


